//
//  WDInAppPurchaseHandleManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/16.
//

#import "WDInAppPurchaseHandleManager.h"
#import "WDAdStatusManager.h"

@implementation WDInAppPurchaseHandleManager
static WDInAppPurchaseHandleManager *_InAppPurchaseHandleManager = nil;
+ (instancetype)shareInAppPurchaseHandleManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _InAppPurchaseHandleManager = [[self alloc] init];
    });
    return _InAppPurchaseHandleManager;
}

- (void)billing_purchase_request:(NSString *)productId
{
    [[WDAdStatusManager shareStatusManager] inAppPurchaseRequestWithEvent:BILLING_EVENT_NAME productId:productId];
}

- (void)billing_purchase_success:(SKPaymentTransaction *)tran
{
    [[WDAdStatusManager shareStatusManager] inAppPurchaseSuccessWithEvent:BILLING_EVENT_NAME productId:tran.payment.productIdentifier];
}

- (void)billing_purchase_failed:(SKPaymentTransaction *)tran errorMsg:(NSString *)msg
{
    [[WDAdStatusManager shareStatusManager] inAppPurchaseFailedWithEvent:BILLING_EVENT_NAME productId:tran.payment.productIdentifier errorType:[self productType:tran.payment.productIdentifier] errorMsg:msg];
}

- (void)billing_Revenue:(SKPaymentTransaction *)tran
{
    NSDictionary *dict = [self skproductInfo:tran.payment.productIdentifier];
//    [[WDAdStatusManager shareStatusManager] inAppPurchaseRevenueWithEvent:BILLING_EVENT_NAME type:[self productType:tran.payment.productIdentifier] source:@"" revenue:[NSString stringWithFormat:@"%@",dict[@"price"]] d_revenue:[WDJsonManager renvenueWithProductId:tran.payment.productIdentifier] currency:[NSString stringWithFormat:@"%@",dict[@"currency"]]];
}

- (void)billing_Revenue_Record:(SKPaymentTransaction *)tran isSuccess:(BOOL)isSuccess errorMsg:(NSString *)errorMsg orderId:(NSString *)orderId
{
    NSDictionary *dict = [self skproductInfo:tran.payment.productIdentifier];
    
    [[WDAdStatusManager shareStatusManager] inAppPurchaseRevenueRecordWithEvent:BILLING_EVENT_NAME type:[self productType:tran.payment.productIdentifier] productName:[NSString stringWithFormat:@"%@",dict[@"title"]] orderId:orderId revenue:[NSString stringWithFormat:@"%@",dict[@"price"]] d_revenue:@"" currency:[NSString stringWithFormat:@"%@",dict[@"currency"]] isSuccess:isSuccess failureReason:errorMsg];
}

- (NSString *)productType:(NSString *)productId
{
    NSString *type = @"0";
//    if ([[WDJsonManager consumableProductIdForSDKConfigFile] containsObject:productId])
//    {
//        type = @"1";
//    }
//    if ([[WDJsonManager nonConsumableProductIdForSDKConfigFile] containsObject:productId] || [[WDJsonManager subscribeProductIdForSDKConfigFile] containsObject:productId])
//    {
//        type = @"2";
//    }
    return type;
}

- (NSDictionary *)skproductInfo:(NSString *)productId
{
    for (SKProduct *pro in self.productIds) {
        NSString *pid = pro.productIdentifier;
        if ([pid isEqualToString:productId])
        {
            NSString *currency = pro.priceLocale.currencyCode;
            NSString *price = [NSString stringWithFormat:@"%@",pro.price];
            return @{@"currency":currency,@"price":price,@"title":pro.localizedTitle};
        }
        
    }
    return @{@"currency":@"",@"price":@"",@"title":@""};
}

@end
